/// <reference path=../data/rsscode.js />
/// <reference path="rsseasy.min.js" />
/// <reference path="rsseasy.app.js" />

$(function () {
    var rss_prikey = Ajax.prototype.rss_prikey;
    var storage = window.localStorage;
    var userobj = JSON.parse(storage.getItem("userdata"));
//    用户信息自动刷新
    function userfresh() {
        storage.userdata = JSON.stringify(userobj);
        userobj = JSON.parse(storage.getItem("userdata"));
    }
    userfresh();
    //用户信息首次刷新
    $('#nickname').text(userobj.nickname);
    if (userobj.sex == 1) {
        $('#sex').text("男");
    } else if (userobj.sex == 2) {
        $('#sex').text("女");
    } else {
        $('#sex').text("");
    }
    $('#grade').text(userobj.grade);
    $('#shengid').text(AreaListData[userobj.shengid][0]);
    $('#schoolid').text(userobj.schoolid);
    //显示提示信息
//    function remind1(msg, button) {   //此部分写在app.js
//        $('#remind1 p').text(msg);
//        $('#remind1 button').text(button);
//    }
    function remind2(msg, button1, button2) {
        $('#remind2 p').text(msg);
        $('#remind2 button:nth-child(2)').text(button1);
        $('#remind2 button:nth-child(3)').text(button2);
        $('.masklayer').show();
        $('#remind2').show();
    }
    //隐藏提示信息
//    $('#remind1 button').click(function () {  //此部分写在app.js
//        $('.masklayer').hide();
//        $('#remind1').hide();
//        $('#remind1 p').text("");
//        $('#remind1 button').text("");
//    });
    //密码显示隐藏
    $('.pwd_show').click(function () {
        if ($(this).next().attr("type") === "text") {
            $(this).next().attr("type", "password");
        } else {
            $(this).next().attr("type", "text");
        }
    });
    //手机号绑定发送验证码按钮
    $('#reg_account,#getpwd_account').bind("input propertychange", function () {
        if ($(this).val().match(/^1[34578]\d{9}$/g)) {
            $(this).parent().next().next().find("button").css({"background": "#ffcc00", "color": "#fff"});
            $(this).parent().next().next().find("button").attr("disabled", null);
        } else {
            $(this).parent().next().next().find("button").css({"background": "rgba(0,0,0,0.3)", "color": "#e3e3e3"});
            $(this).parent().next().next().find("button").attr("disabled", "disabled");
        }
    });
//注册
    var smsisok = ""; //是否已发送验证码
    var verifyisok = false;
    $('#reg_submit').click(function () {
        try {
            if (!$('#reg_account').val()) {
                throw "请输入手机号";
            }
            if (!$('#reg_account').val().match(/^1[34578]\d{9}$/)) {
                throw "请输入正确的手机号";
            }
            if (!$('#reg_account').val()) {
                throw "请输入密码";
            }
            if (!$('#reg_pwd').val().match(/(?!^\d+$)(?!^[a-zA-Z]+$)[0-9a-zA-Z]{6,12}/)) {
                throw "请输入6-12位数字、英文混合密码";
            }
            if (!$('#reg_sms')) {
                throw "请输入验证码";
            }
            if (smsisok === "") {
                throw "请发送验证码";
            }
            if (smsisok !== MD5($('#reg_sms').val() + $('#reg_account').val() + rss_prikey)) {
                throw "请输入正确的验证码";
            }
            verifyisok = true;
            console.log("verifyisok");
        } catch (e) {
            remind1(e, "确定");
        }
    });
//    验证码定时器
    $('#getmessage').click(function () {
        $('#getmessage').css({"background": "rgba(0,0,0,0.3)", "color": "#e3e3e3"});
        $('#getmessage').attr("disabled", "disabled");
        //定时器
        var setTime;
        $('#getmessage').text("60s");
        var time = parseInt($("#getmessage").text());
        setTime = setInterval(function () {
            if (time < 2) {
                clearInterval(setTime);
                $('#getmessage').text("获取验证码");
                $('#getmessage').css({"background": "#ffcc00", "color": "#fff"});
                $('#getmessage').attr("disabled", null);
                return;
            }
            time--;
            $("#getmessage").text(time + "s");
        }, 1000);
        new Ajax("sms/reg.php").keyvalue({"telphone": $('#reg_account').val()}).getJson(function (sms) {
            console.log("sendok");
            if (sms.smstoken) {
                smsisok = sms.smstoken;
                //发送注册信息
                $('#reg_submit').click(function () {
                    if (verifyisok) {
                        $('.loadbox span').text("注册中");
                        $('.masklayer').show();
                        $('.loadbox').show();
                        new Ajax("user/reg.php").keyvalue({"account": $('#reg_account').val(), 'pwd': $('#reg_pwd').val()}).post(function (data) {
                            $('.masklayer').hide();
                            $('.loadbox').hide();
                            var json = eval('(' + data + ')');
                            console.log(json);
                            if (!json.rsscode) {
                                storage.setItem("userdata", JSON.stringify(json));
                                remind1("注册成功", "确定");
                                $('.remind button').click(function () {
                                    location.hash = "#mine";
                                });
                            } else {
                                remind1("注册失败，请重试", "确定");
                            }
                        });
                    }
                });
            }
        });
    });
    //登录操作
    $('#login_submit').click(function () {
        var account = $('#login_account').val();
        var pwd = $('#login_pwd').val();
        try {
            if (!account) {
                throw "请输入手机号";
            }
            if (!account.match(/^1[34578]\d{9}$/)) {
                throw "请输入正确的手机号";
            }
            if (!pwd) {
                throw "请输入密码";
            }
            if (!pwd.match(/(?!^\d+$)(?!^[a-zA-Z]+$)[0-9a-zA-Z]{6,12}/)) {
                throw "请输入6-12位数字、英文混合密码";
            }
            $('.loadbox span').text("正在登录");
            $('.masklayer').show();
            $('.loadbox').show();
            new Ajax("user/login.php").keyvalue({"account": account, 'pwd': pwd}).post(function (data) {
                var json = eval('(' + data + ')');
                console.log(json);
                $('.masklayer').hide();
                $('.loadbox').hide();
                if (!json.rsscode) {
                    storage.setItem("userdata", JSON.stringify(json));
                    userfresh();
                    location.hash = "#mine";
                    $('#mine_nouser').hide();
                    $('#mine_user').show();
                } else {
                    remind1(dictdata.rsscode[json.rsscode], "确定");
                }
            });
        } catch (e) {
            remind1(e, "确定");
        }
    });

    //重置密码

    var smsisok = ""; //是否已发送验证码
    var verifyisok = false;
    $('#getpwd_submit').click(function () { //验证
        try {
            if (!$('#getpwd_account').val()) {
                throw "请输入手机号";
            }
            if (!$('#getpwd_account').val().match(/^1[34578]\d{9}$/)) {
                throw "请输入正确的手机号";
            }
            if (!$('#getpwd_account').val()) {
                throw "请输入密码";
            }
            if (!$('#getpwd_pwd').val().match(/(?!^\d+$)(?!^[a-zA-Z]+$)[0-9a-zA-Z]{6,12}/)) {
                throw "请输入6-12位数字、英文混合密码";
            }
            if (!$('#getpwd_sms')) {
                throw "请输入验证码";
            }
            if (smsisok === "") {
                throw "请发送验证码";
            }
            if (smsisok !== MD5($('#getpwd_sms').val() + $('#getpwd_account').val() + rss_prikey)) {
                throw "请输入正确的验证码";
            }
            verifyisok = true;
            console.log("verifyisok");
        } catch (e) {
            remind1(e, "确定");
        }
    });
    //    验证码定时器
    $('#getmessage2').click(function () {
        $('#getmessage2').css({"background": "rgba(0,0,0,0.3)", "color": "#e3e3e3"});
        $('#getmessage2').attr("disabled", "disabled");
        //定时器
        var setTime;
        $('#getmessage2').text("60s");
        var time = parseInt($("#getmessage2").text());
        setTime = setInterval(function () {
            if (time < 2) {
                clearInterval(setTime);
                $('#getmessage2').text("获取验证码");
                $('#getmessage2').css({"background": "#ffcc00", "color": "#fff"});
                $('#getmessage2').attr("disabled", null);
                return;
            }
            time--;
            $("#getmessage2").text(time + "s");
        }, 1000);
        new Ajax("sms/verify.php").keyvalue({"telphone": $('#getpwd_account').val()}).getJson(function (sms) {
            console.log("sendok");
            if (sms.smstoken) {
                smsisok = sms.smstoken;
                //发送注册信息
                $('#getpwd_submit').click(function () {
                    if (verifyisok) { //根据判断值是否可以发送
                        $('.loadbox span').text("修改中");
                        $('.masklayer').show();
                        $('.loadbox').show();
                        new Ajax("user/getpwd.php").keyvalue({"account": $('#getpwd_account').val(), 'pwd': $('#getpwd_pwd').val()}).post(function (data) {
                            $('.masklayer').hide();
                            $('.loadbox').hide();
                            var json = eval('(' + data + ')');
                            console.log(json);
                            if (!json.rsscode) {
                                storage.setItem("userdata", JSON.stringify(json));
                                remind1("密码修改成功", "确定");
                                $('.remind button').click(function () {
                                    location.hash = "#mine";
                                });
                            } else {
                                remind1("修改失败，请重试", "确定");
                            }
                        });
                    }
                });
            }
        });
    });
    //用户登出
    $('#logout').click(function () {
        remind2("确认退出登录吗?", "确认", "取消");
        $('#remind2 button:nth-child(2)').click(function () {
            storage.removeItem("userdata");
            $('#mine_nouser').show();
            $('#mine_user').hide();
            console.log(storage);
        })
        $('#remind2 button').click(function () {
            $('#masklayer').hide();
            $('#remind2').hide();
        });
    });
    //拨打客服
    $('#findme').click(function () {
        remind2("010-4738284", "取消", "呼叫");
        $('#remind2 button').click(function () {
            $('#masklayer').hide();
            $('#remind2').hide();
        });
    });
    //帮助反馈
    $('#feedbacknum').bind("input propertychange", function () {
        var val = $(this).val();
        num = val.length;
        $('#feedbackspan').text(num + "/250");
    });
    $('#feedbacksubmit').click(function () {
        try {
            $(".masklayer").show();
            $('.loadbox span').text("正在提交");
            $(".loadbox").show();
            var description = $('#feedbacknum').val();
            var telphone = $('#feedbackphone').val();
            if (!description) {
                throw "请输入反馈信息";
            }
            if (!telphone.match(/^1[34578]\d{9}$/)) {
                telphone = null;
            }
            new Ajax("feedback/append.php").keyvalue({"description": description, "telphone": telphone, "myid": userobj.myid}).getJson(function (json) {
                console.log(json);
                $('.masklayer').hide();
                $(".loadbox").hide();
                if (json.id) {
                    remind1("提交成功，我们将尽快处理", "确定");
                } else {
                    remind1("系统错误，请稍候再试！", "确定");
                }
//            清空消息
                $('#feedbacknum').val("");
                $('#feedbackphone').val("");
            });
        } catch (e) {
            remind1(e, "确定");
        }
    });









//用户信息




//修改昵称

    $('#changenickname').click(function () {
        var nickname = $('#nicknameval').val();
        $('#nickname').text(nickname);
        $(this).prev().prev().click();
        new Ajax("user/update.php").keyvalue({"nickname": nickname, "myid": userobj.myid}).getJson(function (json) {
            if (json.nickname) {
                console.log(json);
                userobj.nickname = json.nickname;
                userfresh();
            }
        });
    });
//选择性别
    $('#selectsex').click(function () {
        $('.masklayer').show();
        $('#select_sex').show();
        $('#select_sex div:first-child button').click(function () {
            $('#sex').text($(this).text());
            $('.masklayer').hide();
            $('#select_sex').hide();
            var sextext = $(this).text();
            if (sextext === "男") {
                sex = 1;
            } else if (sextext === "女") {
                sex = 2;
            }
            new Ajax("user/update.php").keyvalue({"sex": sex, "myid": userobj.myid}).getJson(function (json) {
                console.log(json);
                if (json.sex) {
                    userobj.sex = json.sex;
                    userfresh();
                }
            });
        }
        );
        $('#select_sex div:nth-child(2) button').click(function () {
            $('.masklayer').hide();
            $('#select_sex').hide();
        });
    });
//选择地区
    var area = "";
    $.each(AreaListData, function (k, v) {
        if (this[1] === "0") {
            area += '<li>' + this[0] + '</li>';
        }
    });
    $('#select_area article ul').html(area);
    $('#select_area article li').click(function () {
        var sheng = $(this).text();
        console.log(sheng);
        $.each(AreaListData, function (k, v) {
            if (sheng === this[0]) {
                shengid = k;
                return;
            }
        });
        $('#shengid').text(sheng);
        $("#select_area header a").click();
        new Ajax("user/update.php").keyvalue({"myid": userobj.myid, "shengid": shengid}).getJson(function (json) {
            console.log(json);
            if (json.shengid) {
                userobj.shengid = json.shengid;
                userfresh();
            }
        });
    });

//选择年级
    $('#select_grade article li').click(function () {
        var grade = $(this).text();
        $('#grade').text(grade);
        $('#select_grade header a').click();
        new Ajax("user/update.php").keyvalue({"grade": grade, "myid": userobj.myid}).getJson(function (json) {
            console.log(json);
            if (json.grade) {
                userobj.grade = json.grade;
                userfresh();
            }
        });
    });

//学校
    $('#changeschool').click(function () {
        var schoolid = $('#schoolval').val();
        $('#schoolid').text(schoolid);
        $(this).prev().prev().click();
        new Ajax("user/update.php").keyvalue({"schoolid": schoolid, "myid": userobj.myid}).getJson(function (json) {
            console.log(json);
            if (json.schoolid) {
                userobj.schoolid = json.schoolid;
                userfresh();
            }
        });
    });

//标签
    $('#select_label article li').click(function () {
        if ($(".label_select").length < 3) {
            $(this).toggleClass("label_select");
        } else {
            $(this).removeClass("label_select");
        }
    });



//我的个人主页




//环形计时（未完成）(录音功能)
    var time = 10, //时间(s)
            qiehuan = time / 2, //切换:时间/2
            circleProgress = $(".circleProgress >li> div"),
            leftcircle = circleProgress.eq(0),
            rightcircle = circleProgress.eq(1),
            dt = new Date(),
            h2 = $("#circle h2"), //分
            h3 = $("#circle h3"), //秒
            h4 = $("#circle h4"), //毫秒
            tick = 360 / time,
            count = 0;
    var tasktick = setInterval(function ()
    {
        time--;
        dt = new Date(time * 1000);
        h2.html(dt.getMinutes());
        h3.html(dt.getSeconds());
        h4.html(dt.getMilliseconds());
        if (time >= qiehuan) {
            rightcircle.css({"transform": "rotate(" + (-time * tick + 45) + "deg)", "-webkit-transform": "rotate(" + (-time * tick + 45) + "deg)"});
        } else {
            leftcircle.css({"transform": "rotate(" + (-time * tick - 135) + "deg)", "-webkit-transform": "rotate(" + (-time * tick - 135) + "deg)"});
        }
        if (time <= 0) {
            clearInterval(tasktick);
            tasktick = null;
//            console.log("over");
            //倒计时结束，后续处理程序
        }
    }, 1000);


//提现

    function withdrawpermit() { //判断是否可提现
        if ($('#withdraw_account').val() >= 10) {
            $('#withdraw_next').css({"background": "#ffcc00", "color": "#ffffff"});
            $('#withdraw_next').attr("disabled", false);
            $('#withdraw_next').click(function () {
                console.log("click");
                $('.masklayer').show();
                $('#pay').show();
            });
        } else {
            $('#withdraw_next').css({"background": "#e6e6e6", "color": "#4c4c4c"});
            $('#withdraw_next').attr("disabled", "disabled");
        }
    }

    $('#withdrawall').click(function () {   //全部提现
        var max = $("#withdraw_max").text();
        $('#withdraw_account').val(parseInt(max.substr(1)));
        withdrawpermit();
    });

    $('#withdraw_account').bind("input propertychange", function () {  //input金额监听
        var val = parseInt($(this).val());
        $(this).val(val);
        if (val > parseInt($("#withdraw_max").text().substr(1))) {
            $('#withdraw_account').val(parseInt($("#withdraw_max").text().substr(1)));
        }
        withdrawpermit();
    });

//选择支付方式

    $('#pay>div p:nth-child(2),#pay>div p:nth-child(3)').click(function () {
        $(this).siblings().find("img").hide();
        $(this).find("img").show();
    });
    $('#pay button:last-child').click(function () {
        $('#pay').hide();
        $('.masklayer').hide();
    });

//麓遥认证

    $('.auth_sel').bind("input propertychange", function () {  //修改select颜色
        var val = $(this).val();
        if (val !== "选择学位类型") {
            $(this).css({"color": "#333333", "padding-left": "2.4rem"});
        } else {
            $(this).css({"color": "#999999", "padding-left": "1.82rem"});
        }
    });

    $('#authentication_readme').click(function () {   //已阅读协议
        var src = $(this).find("img").attr("src");
        console.log(src);
        if (src === "img/renzheng/readme.png") {
            $(this).find("img").attr("src", "img/renzheng/readme_true.png");
        } else {
            $(this).find("img").attr("src", "img/renzheng/readme.png");
        }
    });















    //邀请码
    $('#invite_submit').click(function () {
        try {
            var inviter = parseInt($('#invite_code').val());
            if (!userobj.myid) {
                throw "请先登陆后再试！";
            }
            new Ajax("user/invite/append.php").keyvalue({"myid": userobj.myid, "inviter": inviter}).getJson(function (json) {
                console.log(json);
                if (json.rsscode) {
                    throw dictdata['rsscode'][json.rsscode];
                }
            });
        } catch (e) {
            console.log(e);
        }
    });
});